<template>
  <div>
    <h2>{{ this.id ? "编辑分类：" : "创建分类：" }}</h2>
    <el-form label-width="80px" :model="form" :rules="rules" ref="ruleForm">
      <el-form-item label="分类名称" prop="classname">
        <el-input style="width:220px" v-model="form.classname"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="createClassify">{{
          this.id ? "修 改" : "确 认"
        }}</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { getClassdetail, getClassadd, getClassedit } from "@/api/index.js";
export default {
  props: ["id"],
  data() {
    return {
      form: {},
      rules: {
        classname: [
          { required: true, message: "请输入分类名称", trigger: "blur" },
        ],
      },
    };
  },
  mounted() {
    console.log(this.id);
    this.id && this.getClassdetail();
  },
  methods: {
    // 创建分类
    createClassify() {
      if (this.id) {
        getClassedit(this.id, this.form).then((res) => {
          if (res.data.status == 200) {
            this.$message.success(res.data.msg);
            this.$router.push("/classifyList");
          }
        });
      } else {
        this.$refs.ruleForm.validate((vali) => {
          if (vali) {
            getClassadd(this.form).then((res) => {
              if (res.status == 200) {
                this.$message.success("创建成功！");
                this.$router.push("/classifyList");
              }
            });
          } else {
            this.$message.error("请将数据补充完整！");
          }
        });
      }
    },
    // 获取分类
    getClassdetail() {
      getClassdetail(this.id).then((res) => {
        this.form = res.data;
      });
    },
  },
};
</script>

<style lang="less" scoped></style>
